# 2024/1/17 14:40 HFL
import unittest
import pytest
from time import sleep

from appium import webdriver
from appium.webdriver.common.mobileby import MobileBy
from appium.webdriver.common.touch_action import TouchAction
from selenium.webdriver.support.wait import WebDriverWait


class TestCaseApp(unittest.TestCase):

    def setUp(self):
        pass

    def tearDown(self):
        pass

    def test_app_test01(self):
        dic = {
            # 被手机的操作系统
            "platformName": "Android",
            # 被手机的操作系统的版本
            "platformVersion": "5.1.1",
            # 被测试APP的包名
            "appPackage": "com.android.settings",
            # 被测试的app的第一个界面的路径
            "appActivity": "com.android.settings.Settings",
            # 设备名称
            "deviceName": "127.0.0.1:62001",
            # 元素定位方法
            "automationName": "UiAutomator2",
            # 输入法
            "uicodeKeyboard": True,
            # 重置输入法
            "resetKeyboard": True,
            # 界面缓存不重置
            "noReset": True,
            # 时间超时时间
            "newCommandTimeout": "3000"
        }

        driver = webdriver.Remote("http://127.0.0.1:4723/wd/hub", dic)
        sleep(1.5)
        driver.find_element(MobileBy.ACCESSIBILITY_ID, '搜索').click()
        sleep(1.5)
        driver.find_element(MobileBy.ANDROID_UIAUTOMATOR, 'text("搜索…")').send_keys('hello')
        sleep(1.5)
        driver.find_element(MobileBy.ID, 'android:id/search_src_text').clear()
        sleep(1.5)
        driver.find_element(MobileBy.ID, 'android:id/search_src_text').send_keys('你好')
        sleep(1.5)
        ele = driver.find_element(MobileBy.ACCESSIBILITY_ID, '收起')
        print("大小：", ele.size)
        print("位置：", ele.location)
        print(ele.get_attribute('content-desc'))
        print(ele.get_attribute('bounds'))
        print(ele.get_attribute('focused'))
        ele.click()
        # 坐标滑动
        # driver.swipe(260, 1170, 260, 870)
        sleep(2)
        ele1 = driver.find_element(MobileBy.ANDROID_UIAUTOMATOR, 'text("电池")')
        ele2 = driver.find_element(MobileBy.ANDROID_UIAUTOMATOR, 'text("显示")')
        driver.scroll(ele1, ele2)
        sleep(1)
        driver.find_element(MobileBy.ANDROID_UIAUTOMATOR, 'text("安全")').click()
        sleep(1)
        driver.find_element(MobileBy.ANDROID_UIAUTOMATOR,'text("屏幕锁定方式")').click()
        sleep(1)
        driver.find_element(MobileBy.ANDROID_UIAUTOMATOR,'text("图案")').click()
        sleep(1)
        action = TouchAction(driver)
        sleep(1)
        action.press(x=180,y=630).move_to(x=534,y=630).move_to(x=540,y=980).move_to(x=540,y=1340).move_to(x=900,y=1334).release().perform()



if __name__ == '__main__':
    pytest.main(['-vs', 'app_test_case01.py', '--html=../report/20240117html.html'])
